#!/bin/bash
set -e

#>>-----------------------------------------------------------------------------------
#顶层项目目录
export PROJECT_TOPDIR=$PWD
#顶层当前配置文件
export CURCFG_FILE=${PROJECT_TOPDIR}/.curconfig.sh
#-------------------------------------------------------------------------------------

#>>env--------------------------------------------------------------------------------
#设置颜色函数
source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.000.colorsetup.sh
#导入公共变量
source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.001.envsetup_common.sh
#导入平台变量
source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.002.envsetup_platform.sh
#-------------------------------------------------------------------------------------

#检查用户输入是否有效
check_choose()
{
	sinfo "检查机型... [开始]"

	#检查设备目录是否存在
	if [ ! -d ${OEMKIT_DEVICES_DIR} ]; then
		serro "查找目录: ${OEMKIT_DEVICES_DIR} [不存在]"
		exit 1
	else
		sdone "查找目录: ${OEMKIT_DEVICES_DIR} [成功]"
	fi

	#检查设备指定型号目录是否存在
	if [ ! -d ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE} ]; then
		serro "查找目录: ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE} [不存在]"
		exit 1
	else
		sdone "查找目录: ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE} [成功]"
	fi

	#检查boot配置文件是否存在
	if [ ! -f ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_CFG_NAME} ]; then
		serro "查找文件: ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_CFG_NAME} [提示]"
	else
		sdone "查找文件: ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_CFG_NAME} [成功]"
	fi

	#检查boot头文件是否存在
	if [ ! -f ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_HAD_NAME} ]; then
		serro "查找文件: ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_HAD_NAME} [不存在]"
	else
		sdone "查找文件: ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_HAD_NAME} [成功]"
	fi

	#检查kern配置文件是否存在
	if [ ! -f ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_KERN_CFG_NAME} ]; then
		serro "查找文件: ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_KERN_CFG_NAME} [不存在]"
	else
		sdone "查找文件: ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_KERN_CFG_NAME} [成功]"
	fi
	sdone "检查机型... [成功]"
}

#更新相关配置文件
update_configs()
{
	sinfo "更新配置文件... [开始]"
	
	update_copy_env
	update_boot_env
	update_kern_env
	update_knfs_env
	update_rtfs_env

	sinfo "#boot_______________________________________________________"
	if [ -f ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_CFG_NAME} ]; then
		echo -e "cp\t${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_CFG_NAME}\n\t${SYSKIT_BOOT_CFG_PATH}"
		cp ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_CFG_NAME} ${SYSKIT_BOOT_CFG_PATH}
	fi
	
	if [ -f ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_HAD_NAME} ]; then
		echo -e "cp\t${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_HAD_NAME}\n\t${SYSKIT_BOOT_HAD_PATH}"
		cp ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_HAD_NAME} ${SYSKIT_BOOT_HAD_PATH}
	fi
	
	if [ -f ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_ENV_NAME} ]; then
		echo -e "cp\t${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_ENV_NAME}\n\t${SYSKIT_BOOT_ENV_PATH}"
		cp ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_BOOT_ENV_NAME} ${SYSKIT_BOOT_ENV_PATH}
	fi
	sinfo "#-------------------------------------------------------boot"
	
	sinfo "#kern_______________________________________________________"
	if [ -f ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_KERN_CFG_NAME} ]; then
		echo -e "cp\t${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_KERN_CFG_NAME}\n\t${SYSKIT_KERN_CFG_PATH}"
		cp ${OEMKIT_DEVICES_DIR}/${OEMDEV_CUR_TYPE}/${OEMDEV_KERN_CFG_NAME} ${SYSKIT_KERN_CFG_PATH}
	fi
	sinfo "#------------------------------------------------------kernel"

	#buildroot
	#echo -e "echo U${OEMDEV_CUR_TYPE} > ${SYSKIT_KNFS_RAWDIR_PATH}/etc/hostname"
	#echo "U${OEMDEV_CUR_TYPE}" > ${SYSKIT_KNFS_RAWDIR_PATH}/etc/hostname

	sdone "更新配置文件... [完毕]"
}

#创建新的软连接
update_links()
{
	sinfo "创建快捷方式... [开始]"

	rm z.src.${OEMDEV_BOOT_CFG_PREFIX} -rf
	rm z.src.kern    -rf
	rm z.src.brts    -rf 
	rm z.src.knfs    -rf
	rm z.src.rtfs    -rf
	rm z.src.rootfs  -rf

	rm z.out.${OEMDEV_BOOT_CFG_PREFIX} -rf
	rm z.out.kern    -rf
	rm z.out.brts    -rf 
	rm z.out.knfs    -rf
	rm z.out.rtfs    -rf
	rm z.out.rootfs  -rf
	
	if [ -d ${SYSKIT_BOOT_RAWDIR_PATH} ]; then
		sinfo "ln -s\t${SYSKIT_BOOT_RAWDIR_PATH} \n\tz.src.${OEMDEV_BOOT_CFG_PREFIX}"
		ln -s ${SYSKIT_BOOT_RAWDIR_PATH} z.src.${OEMDEV_BOOT_CFG_PREFIX}
	fi

	if [ -d ${SYSKIT_KERN_RAWDIR_PATH} ]; then
		sinfo "ln -s\t${SYSKIT_KERN_RAWDIR_PATH}  \n\tz.src.kern"
		ln -s ${SYSKIT_KERN_RAWDIR_PATH}        z.src.kern
	fi

#	if [ -d ${SYSKIT_BRTS_RAWDIR_PATH} ]; then
#		sinfo "ln -s\t${SYSKIT_BRTS_RAWDIR_PATH}   \n\tz.src.brts"
#		ln -s ${SYSKIT_BRTS_RAWDIR_PATH}        z.src.brts
#	fi

	if [ -d ${SYSKIT_KNFS_RAWDIR_PATH} ]; then
		sinfo "ln -s\t${SYSKIT_KNFS_RAWDIR_PATH}  \n\tz.src.knfs"
		ln -s ${SYSKIT_KNFS_RAWDIR_PATH}      z.src.knfs
	fi

	if [ -d ${SYSKIT_RTFS_RAWDIR_PATH} ]; then
		sinfo "ln -s\t${SYSKIT_RTFS_RAWDIR_PATH}  \n\tz.src.rtfs"
		ln -s ${SYSKIT_RTFS_RAWDIR_PATH}      z.src.rtfs
	fi

	if [ -d ${SYSKIT_BOOT_OUT_PATH} ]; then
		sinfo "ln -s\t${SYSKIT_BOOT_OUT_PATH}  \n\tz.out.${OEMDEV_BOOT_CFG_PREFIX}"
		ln -s ${SYSKIT_BOOT_OUT_PATH}        z.out.${OEMDEV_BOOT_CFG_PREFIX}
	fi

	if [ -d ${SYSKIT_KERN_OUT_PATH} ]; then
		sinfo "ln -s\t${SYSKIT_KERN_OUT_PATH}  \n\tz.out.kern"
		ln -s ${SYSKIT_KERN_OUT_PATH}        z.out.kern
	fi

	#	if [ -d ${SYSKIT_BRTS_OUT_PATH} ]; then
#		sinfo "ln -s\t${SYSKIT_BRTS_OUT_PATH}   \n\tz.out.brts"
#		ln -s ${SYSKIT_BRTS_OUT_PATH}        z.out.brts
#	fi

	if [ -d ${SYSKIT_KNFS_OUT_PATH} ]; then
		sudo rm -rf  ${SYSKIT_KNFS_OUT_PATH}
	fi
	sudo cp -rfp ${SYSKIT_KNFS_RAWDIR_PATH} ${SYSKIT_KNFS_OUT_PATH}
	if [ -d ${SYSKIT_KNFS_OUT_PATH} ]; then
		sinfo "ln -s\t${SYSKIT_KNFS_OUT_PATH} \n\tz.out.knfs"
		ln -s ${SYSKIT_KNFS_OUT_PATH}      z.out.knfs
	fi
	
	if [ -d ${SYSKIT_RTFS_OUT_PATH} ]; then
		sudo rm -rf  ${SYSKIT_RTFS_OUT_PATH}
	fi
	sudo cp -rfp ${SYSKIT_RTFS_RAWDIR_PATH} ${SYSKIT_RTFS_OUT_PATH}
	if [ -d ${SYSKIT_RTFS_OUT_PATH} ]; then
		sinfo "ln -s\t${SYSKIT_RTFS_OUT_PATH} \n\tz.out.rtfs"
		ln -s ${SYSKIT_RTFS_OUT_PATH}      z.out.rtfs
	fi
	
	sdone "创建快捷方式... [完毕]"
}

#根据配置计算MTD分区大小
#boot，引导区，    存放UBOOT
#knfs，内核区，    存放kernel和ramdisk
#rtfs，文件系统区，存放只读文件系统
#ovfs，文件系统区，存放用户文件系统
#extd，扩展区，存放比较大的库例如QT
#root, 用户区，存放用户程序和依赖资源
#data, 数据区，存放用户重要数据
#vend, 出厂区，存放出厂设置和升级固件
#分区配额为0表示不启用该分区
update_mtd_patsize()
{
	local ret=0
	export MTDSUB_BOOT=0
	export MTDSUB_KNFS=0
	export MTDSUB_RTFS=0
	export MTDSUB_OVFS=0
	export MTDSUB_EXTD=0
	export MTDSUB_ROOT=0
	export MTDSUB_DATA=0
	export MTDSUB_VEND=0

	#UBOOT大小始终为1MB
	MTDSUB_BOOT=1
	if [ $OEMDEV_CUR_MTDHDSIZ -eq 16 ]; then
		#对低于16MB的SPI FLASH存储器

		if [ "x$OEMDEV_CUR_MTDFSTYP" = "xUBIFS" ]; then
			perro "当前存储容量:${OEMDEV_CUR_MTDHDSIZ}(MB)，无法支持UBIFS文件系统"
			perro "请尝试YAFFS2或者JFFS2!"
			return 1
		fi
		#knfs分区尽量小
		MTDSUB_KNFS=5
		#extd分区需最小
		if [ "x$OEMDEV_CUR_MTDFSPAT" = "xUSRMAX" ]; then
			#用户区最大策略
			MTDSUB_EXTD=0
			MTDSUB_ROOT=7
			MTDSUB_DATA=1
			MTDSUB_VEND=2
		elif [ "x$OEMDEV_CUR_MTDFSPAT" = "xDATMAX" ]; then
            #数据区最大策略
			MTDSUB_EXTD=0
			MTDSUB_ROOT=2
			MTDSUB_DATA=6
			MTDSUB_VEND=2
		else
            #默认采用平均分区策略
			MTDSUB_EXTD=0
			MTDSUB_ROOT=4
			MTDSUB_DATA=4
			MTDSUB_VEND=2
		fi
	else
		#动态确定是否有自定义MTD分区
		is_mtdsub_self_defined=`eval echo '$'MTDSUB_SELF_"${OEMDEV_CUR_TYPE}"`
		
		if [ "x${is_mtdsub_self_defined}" != "xyes" ]; then
			sinfo "自动计算MTD分区 ... "
			#knfs分区用于内核和内核文件系统
			MTDSUB_KNFS=16
			if [ "x$OEMDEV_CUR_MTDFSTYP" = "xUBIFS" ]; then
				#UBIFS分区大小不能小于8MB
				if [ "x$OEMDEV_CUR_MTDFSPAT" = "xUSRMAX" ]; then
					#不带OTA升级
					#用户区最大策略
					MTDSUB_EXTD=0
					MTDSUB_DATA=16
					MTDSUB_VEND=17
					let MTDSUB_ROOT=$[ ${OEMDEV_CUR_MTDHDSIZ} - $[ ${MTDSUB_BOOT} + ${MTDSUB_KNFS} + ${MTDSUB_EXTD} + ${MTDSUB_DATA} + ${MTDSUB_VEND} ] ]
				elif [ "x$OEMDEV_CUR_MTDFSPAT" = "xDATMAX" ]; then
    	   		    #不带OTA升级
					#数据区最大策略
					MTDSUB_EXTD=0
					MTDSUB_ROOT=16
					MTDSUB_VEND=17
					let MTDSUB_DATA=$[ ${OEMDEV_CUR_MTDHDSIZ} - $[ ${MTDSUB_BOOT} + ${MTDSUB_KNFS} + ${MTDSUB_EXTD} + ${MTDSUB_ROOT} + ${MTDSUB_VEND} ] ]
				else
	           		#不带OTA升级
					#默认采用平均分区策略
					MTDSUB_EXTD=0
					MTDSUB_VEND=17
					let MTDSUB_ROOT=$[ $[ ${OEMDEV_CUR_MTDHDSIZ} - $[ ${MTDSUB_BOOT} + ${MTDSUB_KNFS} + ${MTDSUB_EXTD} + ${MTDSUB_VEND} ] ] / 2 ]
					let MTDSUB_DATA=${MTDSUB_ROOT}
				fi
			else
				if [ "x$OEMDEV_CUR_MTDFSPAT" = "xUSRMAX" ]; then
	           		#不带OTA升级
					#用户区最大策略
					MTDSUB_EXTD=0
					MTDSUB_DATA=10
					MTDSUB_VEND=11
					let MTDSUB_ROOT=$[ ${OEMDEV_CUR_MTDHDSIZ} - $[ ${MTDSUB_BOOT} + ${MTDSUB_KNFS} + ${MTDSUB_EXTD} + ${MTDSUB_DATA} + ${MTDSUB_VEND} ] ]
				elif [ "x$OEMDEV_CUR_MTDFSPAT" = "xUMFOTA" ]; then
					#带有OTA升级
					#用户区最大策略
					MTDSUB_KNFS=8
					MTDSUB_RTFS=16
					MTDSUB_OVFS=0
					MTDSUB_EXTD=0
					MTDSUB_DATA=10
					MTDSUB_VEND=11
					let MTDSUB_ROOT=$[ ${OEMDEV_CUR_MTDHDSIZ} - $[ ${MTDSUB_BOOT} + \
						${MTDSUB_KNFS}    + \
						${MTDSUB_RTFS}    + \
						${MTDSUB_OVFS}    + \
						${MTDSUB_EXTD}    + \
						${MTDSUB_DATA}    + \
						${MTDSUB_VEND} ] ]
				elif [ "x$OEMDEV_CUR_MTDFSPAT" = "xDATMAX" ]; then
	            	#不带OTA升级
    	        	#数据区最大策略
					MTDSUB_EXTD=0
					MTDSUB_ROOT=10
					MTDSUB_VEND=11
					let MTDSUB_DATA=$[ ${OEMDEV_CUR_MTDHDSIZ} - $[ ${MTDSUB_BOOT} + ${MTDSUB_KNFS} + ${MTDSUB_EXTD} + ${MTDSUB_ROOT} + ${MTDSUB_VEND} ] ]
				else
	   	   		    #不带OTA升级
	   		        #默认采用平均分区策略
					MTDSUB_EXTD=0
					MTDSUB_VEND=11
					let MTDSUB_ROOT=$[ $[ ${OEMDEV_CUR_MTDHDSIZ} - $[ ${MTDSUB_BOOT} + ${MTDSUB_KNFS} + ${MTDSUB_EXTD} + ${MTDSUB_VEND} ] ] / 2 ]
					let MTDSUB_DATA=${MTDSUB_ROOT}
				fi
			fi
		else
			swarn "使用自定义MTD分区 ..."
			#动态读取MTD分区大小
			export MTDSUB_BOOT=`eval echo '$'MTDSUB_BOOT_"${OEMDEV_CUR_TYPE}"`
			export MTDSUB_KNFS=`eval echo '$'MTDSUB_KNFS_"${OEMDEV_CUR_TYPE}"`
			export MTDSUB_RTFS=`eval echo '$'MTDSUB_RTFS_"${OEMDEV_CUR_TYPE}"`
			export MTDSUB_OVFS=`eval echo '$'MTDSUB_OVFS_"${OEMDEV_CUR_TYPE}"`
			export MTDSUB_EXTD=`eval echo '$'MTDSUB_EXTD_"${OEMDEV_CUR_TYPE}"`
			export MTDSUB_ROOT=`eval echo '$'MTDSUB_ROOT_"${OEMDEV_CUR_TYPE}"`
			export MTDSUB_DATA=`eval echo '$'MTDSUB_DATA_"${OEMDEV_CUR_TYPE}"`
			export MTDSUB_VEND=`eval echo '$'MTDSUB_VEND_"${OEMDEV_CUR_TYPE}"`
		fi

		if [ "x$OEMDEV_CUR_MTDFSTYP" = "xUBIFS" ]; then
			if [ ${MTDSUB_EXTD} -lt 8 ]; then
				if [ ${MTDSUB_EXTD} -eq 0 ]; then
					swarn "当前extd分区配额:   0(MB)，系统将不会为之分配MTD分区"
					swarn "当前extd分区配额:   0(MB)，副作用是无法存放扩展资源"
				else
					serro "当前extd分区配额:${MTDSUB_EXTD}(MB)，无法支持UBIFS文件系统(所需最小配额8MB)"
					serro "请重新调整分区配额!"
					return 1
				fi
			fi
			if [ ${MTDSUB_ROOT} -lt 8 ]; then
				if [ ${MTDSUB_ROOT} -eq 0 ]; then
					swarn "当前root分区配额:   0(MB)，系统将不会为之分配MTD分区"
					swarn "当前root分区配额:   0(MB)，副作用是/root目录数据掉电后不保存"
				else
					serro "当前root分区配额:${MTDSUB_ROOT}(MB)，无法支持UBIFS文件系统(所需最小配额8MB)"
					serro "请重新调整分区配额!"
					return 1
				fi
			fi
			if [ ${MTDSUB_DATA} -lt 8 ]; then
				if [ ${MTDSUB_DATA} -eq 0 ]; then
					swarn "当前data分区配额:   0(MB)，系统将不会为之分配MTD分区"
					swarn "当前data分区配额:   0(MB)，副作用是/data目录数据掉电后不保存"
				else
					serro "当前data分区配额:${MTDSUB_DATA}(MB)，无法支持UBIFS文件系统(所需最小配额8MB)"
					serro "请重新调整分区配额!"
					return 1
				fi
			fi
			if [ ${MTDSUB_VEND} -lt 8 ]; then
				if [ ${MTDSUB_VEND} -eq 0 ]; then
					swarn "当前vend分区配额:   0(MB)，系统将不会为之分配MTD分区"
					swarn "当前vend分区配额:   0(MB)，副作用是无法进行恢复出厂设置和固件升级"
				else
					serro "当前vend分区配额:${MTDSUB_VEND}(MB)，无法支持UBIFS文件系统(所需最小配额8MB)"
					serro "请重新调整分区配额!"
					return 1
				fi
			fi
		else
			if [ ${MTDSUB_EXTD} -eq 0 ]; then
				swarn "当前extd分区配额:   0(MB)，系统将不会为之分配MTD分区"
				swarn "当前extd分区配额:   0(MB)，副作用是无法存放扩展资源"
			fi
			if [ ${MTDSUB_ROOT} -eq 0 ]; then
				swarn "当前root分区配额:   0(MB)，系统将不会为之分配MTD分区"
				swarn "当前root分区配额:   0(MB)，副作用是/root目录数据掉电后不保存"
			fi
			if [ ${MTDSUB_DATA} -eq 0 ]; then
				swarn "当前data分区配额:   0(MB)，系统将不会为之分配MTD分区"
				swarn "当前data分区配额:   0(MB)，副作用是/data目录数据掉电后不保存"
			fi
			if [ ${MTDSUB_VEND} -eq 0 ]; then
				swarn "当前vend分区配额:   0(MB)，系统将不会为之分配MTD分区"
				swarn "当前vend分区配额:   0(MB)，副作用是无法进行恢复出厂设置和固件升级"
			fi
		fi
	fi

	export MTDSUB_BOOT
	export MTDSUB_KNFS
	export MTDSUB_RTFS
	export MTDSUB_OVFS
	export MTDSUB_EXTD
	export MTDSUB_ROOT
	export MTDSUB_DATA
	export MTDSUB_VEND

	sinfo "-------------------------------------------------------------------------------------"
	sinfo "分区策略: ${OEMDEV_CUR_MTDFSPAT}"
	sinfo "存储容量: ${OEMDEV_CUR_MTDHDSIZ}(MB)"
	sinfo "容量配额: "
	printf "[boot分区]: %03d(MB)\n" ${MTDSUB_BOOT}
	printf "[knfs分区]: %03d(MB)\n" ${MTDSUB_KNFS}
	printf "[rtfs分区]: %03d(MB)\n" ${MTDSUB_RTFS}
	printf "[ovfs分区]: %03d(MB)\n" ${MTDSUB_OVFS}
	printf "[extd分区]: %03d(MB)\n" ${MTDSUB_EXTD}
	printf "[root分区]: %03d(MB)\n" ${MTDSUB_ROOT}
	printf "[data分区]: %03d(MB)\n" ${MTDSUB_DATA}
	printf "[vend分区]: %03d(MB)\n" ${MTDSUB_VEND}
	sinfo "-------------------------------------------------------------------------------------"
	
	let need_mtdsize_total=$[ ${MTDSUB_BOOT} + \
					${MTDSUB_KNFS}    + \
					${MTDSUB_RTFS}    + \
					${MTDSUB_OVFS}    + \
					${MTDSUB_EXTD}    + \
					${MTDSUB_ROOT}    + \
					${MTDSUB_DATA}    + \
					${MTDSUB_VEND} ] 

	printf "配额总需: %03d(MB)\n" ${need_mtdsize_total}
	if [ ${need_mtdsize_total} -gt ${OEMDEV_CUR_MTDHDSIZ} ]; then
		sinfo "-------------------------------------------------------------------------------------"
		perro "当前存储容量:${OEMDEV_CUR_MTDHDSIZ}(MB)，无法满足配额总需:${need_mtdsize_total}(MB)"
		perro "请重新调整分区配额!!!"
		ret=1
	fi
	sinfo "-------------------------------------------------------------------------------------"

	if [ $ret -eq 0 ]; then
		cfgfile=${CURCFG_FILE}
		if [ ! -f ${cfgfile} ]; then
			serro "检查当前配置文件... [失败]"
			exit 1
		fi
		echo -e " "  >> ${cfgfile}
		echo "export OEMDEV_MTDSUB_BOOT=${MTDSUB_BOOT}"  >> ${cfgfile}
		echo "export OEMDEV_MTDSUB_KNFS=${MTDSUB_KNFS}"  >> ${cfgfile}
		echo "export OEMDEV_MTDSUB_RTFS=${MTDSUB_RTFS}"  >> ${cfgfile}
		echo "export OEMDEV_MTDSUB_OVFS=${MTDSUB_OVFS}"  >> ${cfgfile}
		echo "export OEMDEV_MTDSUB_EXTD=${MTDSUB_EXTD}"  >> ${cfgfile}
		echo "export OEMDEV_MTDSUB_ROOT=${MTDSUB_ROOT}"  >> ${cfgfile}
		echo "export OEMDEV_MTDSUB_DATA=${MTDSUB_DATA}"  >> ${cfgfile}
		echo "export OEMDEV_MTDSUB_VEND=${MTDSUB_VEND}"  >> ${cfgfile}
		sdone "写入MTD分区容量配置 [完成]"
	fi
	return $ret
}

#更新MTD索引
update_mtd_index()
{
	cfgfile=${CURCFG_FILE}
	if [ ! -f ${cfgfile} ]; then
		serro "检查当前配置文件... [失败]"
		exit 1
	fi

	curidx=0
	echo -e " "  >> ${cfgfile}
	if [ $MTDSUB_BOOT -ne 0 ]; then
		curidx=0
		export OEMDEV_MTDIDX_BOOT=${curidx}
		echo "export OEMDEV_MTDIDX_BOOT=${curidx}" >> ${cfgfile}
	fi
	if [ $MTDSUB_KNFS -ne 0 ]; then
		let curidx=curidx+1
		export OEMDEV_MTDIDX_KNFS=${curidx}
		echo "export OEMDEV_MTDIDX_KNFS=${curidx}" >> ${cfgfile}
	fi

	if [ $MTDSUB_RTFS -ne 0 ]; then
		let curidx=curidx+1
		export OEMDEV_MTDIDX_RTFS=${curidx}
		echo "export OEMDEV_MTDIDX_RTFS=${curidx}" >> ${cfgfile}
	fi

	if [ $MTDSUB_OVFS -ne 0 ]; then
		let curidx=curidx+1
		export OEMDEV_MTDIDX_OVFS=${curidx}
		echo "export OEMDEV_MTDIDX_OVFS=${curidx}" >> ${cfgfile}
	fi

	if [ $MTDSUB_EXTD -ne 0 ]; then
		let curidx=curidx+1
		export OEMDEV_MTDIDX_EXTD=${curidx}
		echo "export OEMDEV_MTDIDX_EXTD=${curidx}" >> ${cfgfile}
	fi


	if [ $MTDSUB_ROOT -ne 0 ]; then
		let curidx=curidx+1
		export OEMDEV_MTDIDX_ROOT=${curidx}
		echo "export OEMDEV_MTDIDX_ROOT=${curidx}" >> ${cfgfile}
	fi
	if [ $MTDSUB_DATA -ne 0 ]; then
		let curidx=curidx+1
		export OEMDEV_MTDIDX_DATA=${curidx}
		echo "export OEMDEV_MTDIDX_DATA=${curidx}" >> ${cfgfile}
	fi
	if [ $MTDSUB_VEND -ne 0 ]; then
		let curidx=curidx+1
		export OEMDEV_MTDIDX_VEND=${curidx}
		echo "export OEMDEV_MTDIDX_VEND=${curidx}" >> ${cfgfile}
	fi
	sdone "写入MTD分区索引配置 [完成]"
	return 0
}

#更新UBI索引
update_ubi_index()
{
	cfgfile=${CURCFG_FILE}
	if [ ! -f ${cfgfile} ]; then
		serro "检查当前配置文件... [失败]"
		exit 1
	fi
	curidx=0
	echo -e " "  >> ${cfgfile}
	if [ $MTDSUB_RTFS -ne 0 ]; then
		export OEMDEV_UBIIDX_RTFS=${curidx}
		echo "export OEMDEV_UBIIDX_RTFS=${curidx}" >> ${cfgfile}
	fi

	if [ $MTDSUB_OVFS -ne 0 ]; then
		let curidx=curidx+1
		export OEMDEV_UBIIDX_OVFS=${curidx}
		echo "export OEMDEV_UBIIDX_OVFS=${curidx}" >> ${cfgfile}
	fi

	if [ $MTDSUB_EXTD -ne 0 ]; then
		let curidx=curidx+1
		export OEMDEV_UBIIDX_EXTD=${curidx}
		echo "export OEMDEV_UBIIDX_EXTD=${curidx}" >> ${cfgfile}
	fi

	if [ $MTDSUB_ROOT -ne 0 ]; then
		let curidx=curidx+1
		export OEMDEV_UBIIDX_ROOT=${curidx}
		echo "export OEMDEV_UBIIDX_ROOT=${curidx}" >> ${cfgfile}
	fi
	if [ $MTDSUB_DATA -ne 0 ]; then
		let curidx=curidx+1
		export OEMDEV_UBIIDX_DATA=${curidx}
		echo "export OEMDEV_UBIIDX_DATA=${curidx}" >> ${cfgfile}
	fi
	if [ $MTDSUB_VEND -ne 0 ]; then
		let curidx=curidx+1
		export OEMDEV_UBIIDX_VEND=${curidx}
		echo "export OEMDEV_UBIIDX_VEND=${curidx}" >> ${cfgfile}
	fi
	sdone "写入UBI分区索引配置 [完成]"
	return 0
}

#创建当前配置文件
create_curconfig()
{

	sinfo "创建当前配置文件... [开始]"
	echo "#!/bin/bash"                                  >  ${CURCFG_FILE}
	echo "export OEMDEV_CUR_INDEX=${OEMDEV_CUR_INDEX}"	>> ${CURCFG_FILE}
	echo "export OEMDEV_CUR_TYPE=${OEMDEV_CUR_TYPE}"	>> ${CURCFG_FILE}
	echo "export OEMDEV_CUR_NOTE=${OEMDEV_CUR_NOTE}"	>> ${CURCFG_FILE}
	echo ""                                                   >> ${CURCFG_FILE}
	echo "export OEMDEV_CUR_CPUCODE=${OEMDEV_CUR_CPUCODE}"	  >> ${CURCFG_FILE}
	echo "export OEMDEV_CUR_APPAREA=${OEMDEV_CUR_APPAREA}"	  >> ${CURCFG_FILE}
	echo ""                                                   >> ${CURCFG_FILE}
	echo "export OEMDEV_CUR_BOOT_RAWDIR_NAME=${OEMDEV_CUR_BOOT_RAWDIR_NAME}" >> ${CURCFG_FILE}
	echo "export OEMDEV_CUR_KERN_RAWDIR_NAME=${OEMDEV_CUR_KERN_RAWDIR_NAME}" >> ${CURCFG_FILE}
	echo "export OEMDEV_CUR_KNFS_RAWDIR_NAME=${OEMDEV_CUR_KNFS_RAWDIR_NAME}" >> ${CURCFG_FILE}
	echo "export OEMDEV_CUR_RTFS_RAWDIR_NAME=${OEMDEV_CUR_RTFS_RAWDIR_NAME}" >> ${CURCFG_FILE}
	echo ""                                                   >> ${CURCFG_FILE}
	echo "export OEMDEV_CUR_MTDHDSIZ=${OEMDEV_CUR_MTDHDSIZ}"  >> ${CURCFG_FILE}
	echo "export OEMDEV_CUR_MTDFSTYP=${OEMDEV_CUR_MTDFSTYP}"  >> ${CURCFG_FILE}
	echo "export OEMDEV_CUR_MTDFSPAT=${OEMDEV_CUR_MTDFSPAT}"  >> ${CURCFG_FILE}
	echo "export OEMDEV_CUR_MTDHDTYP=${OEMDEV_CUR_MTDHDTYP}"  >> ${CURCFG_FILE}
	echo ""                                                   >> ${CURCFG_FILE}

	update_mtd_patsize
	if [ $? -ne 0 ]; then
		return 1
	fi
		
	update_mtd_index
	if [ $? -ne 0 ]; then
		return 1
	fi

	if [ "x${OEMDEV_CUR_MTDFSTYP}" == "xUBIFS" ]; then
		update_ubi_index
		if [ $? -ne 0 ]; then
			return 1
		fi
	fi

	chmod 777 ${CURCFG_FILE}
	sinfo "创建当前配置文件... [完毕]"
	sinfo "当前配置文件内容:"
	sinfo "-------------------------------------------------------------------------------------"
	cat ${CURCFG_FILE}
	sinfo "-------------------------------------------------------------------------------------"
}

show_usage()
{
	sinfo "-----------------------------------------------------------------------------------------------------------------"
	sinfo "使用说明:"
	sinfo "-----------------------------------------------------------------------------------------------------------------"

	sinfo "  ${OEMDEV_TITLES_LISTS[0]}  \c"
	sinfo "  ${OEMDEV_TITLES_LISTS[1]}  \c"
	sinfo "  ${OEMDEV_TITLES_LISTS[2]}  \c"
	sinfo "  ${OEMDEV_TITLES_LISTS[3]}  \c"
	sinfo "  ${OEMDEV_TITLES_LISTS[4]}  \c"
	sinfo "  ${OEMDEV_TITLES_LISTS[5]}  \c"
	sinfo "  ${OEMDEV_TITLES_LISTS[6]}  \c"
	sinfo "  ${OEMDEV_TITLES_LISTS[7]}  \c"
	sinfo "  ${OEMDEV_TITLES_LISTS[8]}  \c"
	sinfo ""
	sinfo "-----------------------------------------------------------------------------------------------------------------"
	for OEMDEV_INDEX in ${OEMDEV_TYPE_INDEX[@]}
	do
		sinfo "     ${OEMDEV_INDEX}     \c"
		sinfo "    ${OEMDEV_TYPE_LISTS[${OEMDEV_INDEX}]}    \c"     
		sinfo "    ${OEMDEV_CPUCODE_LISTS[${OEMDEV_INDEX}]}   \c"     
		sinfo "    ${OEMDEV_APPAREA_LISTS[${OEMDEV_INDEX}]}    \c"     
		sinfo "    ${OEMDEV_MTDHDTYP_LISTS[${OEMDEV_INDEX}]}  \c"
		sinfo "    ${OEMDEV_MTDHDSIZ_LISTS[${OEMDEV_INDEX}]}    \c"
		sinfo "    ${OEMDEV_MTDFSTYP_LISTS[${OEMDEV_INDEX}]}   \c"
		sinfo "    ${OEMDEV_MTDFSPAT_LISTS[${OEMDEV_INDEX}]} \c"
		sinfo "    ${OEMDEV_TYPE_NOTES[${OEMDEV_INDEX}]}\c"
		sinfo ""
	done
	sinfo "-----------------------------------------------------------------------------------------------------------------"
}

main_proc()
{	
	if [ -z "$1" ]; then
		show_usage
		exit 1
	else
		sinfo "-------------------------------------------------------------------------------------"
		if [ -n "$1" ]; then
			sinfo "  ${OEMDEV_TITLES_LISTS[0]}  \c"
			sinfo "  ${OEMDEV_TITLES_LISTS[1]}  \c"
			sinfo "  ${OEMDEV_TITLES_LISTS[2]}  \c"
			sinfo "  ${OEMDEV_TITLES_LISTS[3]}  \c"
			sinfo "  ${OEMDEV_TITLES_LISTS[4]}  \c"
			sinfo "  ${OEMDEV_TITLES_LISTS[5]}  \c"
			sinfo "  ${OEMDEV_TITLES_LISTS[6]}  \c"
			sinfo ""
			sinfo "-------------------------------------------------------------------------------------"
			for OEMDEV_INDEX in ${OEMDEV_TYPE_INDEX[@]}
			do
				if [ "x$OEMDEV_INDEX" = "x$1" ]; then
					sdone "     ${OEMDEV_INDEX}     \c"
					sdone "    ${OEMDEV_TYPE_LISTS[${OEMDEV_INDEX}]}    \c"     
					sdone "    ${OEMDEV_CPUCODE_LISTS[${OEMDEV_INDEX}]}   \c"     
					sdone "    ${OEMDEV_APPAREA_LISTS[${OEMDEV_INDEX}]}    \c"     
					sdone "    ${OEMDEV_MTDHDTYP_LISTS[${OEMDEV_INDEX}]}  \c"
					sdone "    ${OEMDEV_MTDHDSIZ_LISTS[${OEMDEV_INDEX}]}    \c"
					sdone "    ${OEMDEV_MTDFSTYP_LISTS[${OEMDEV_INDEX}]}   \c"
					sdone "    ${OEMDEV_MTDFSPAT_LISTS[${OEMDEV_INDEX}]} \c"
					sdone "    ${OEMDEV_TYPE_NOTES[${OEMDEV_INDEX}]}\c"
					sdone "\t  [已选定]"

					export OEMDEV_CUR_INDEX=${OEMDEV_INDEX} 
					export OEMDEV_CUR_TYPE=${OEMDEV_TYPE_LISTS[${OEMDEV_INDEX}]} 
					export OEMDEV_CUR_CPUCODE=${OEMDEV_CPUCODE_LISTS[${OEMDEV_INDEX}]} 
					export OEMDEV_CUR_APPAREA=${OEMDEV_APPAREA_LISTS[${OEMDEV_INDEX}]} 
					
					export OEMDEV_CUR_BOOT_RAWDIR_NAME=${OEMDEV_BOOT_RAWDIR_NAME_LISTS[${OEMDEV_INDEX}]} 
					export OEMDEV_CUR_KERN_RAWDIR_NAME=${OEMDEV_KERN_RAWDIR_NAME_LISTS[${OEMDEV_INDEX}]} 
					export OEMDEV_CUR_KNFS_RAWDIR_NAME=${OEMDEV_KNFS_RAWDIR_NAME_LISTS[${OEMDEV_INDEX}]} 
					export OEMDEV_CUR_RTFS_RAWDIR_NAME=${OEMDEV_RTFS_RAWDIR_NAME_LISTS[${OEMDEV_INDEX}]} 

					export OEMDEV_CUR_MTDHDTYP=${OEMDEV_MTDHDTYP_LISTS[${OEMDEV_INDEX}]}
					export OEMDEV_CUR_MTDHDSIZ=${OEMDEV_MTDHDSIZ_LISTS[${OEMDEV_INDEX}]}
					export OEMDEV_CUR_MTDFSTYP=${OEMDEV_MTDFSTYP_LISTS[${OEMDEV_INDEX}]}
					export OEMDEV_CUR_MTDFSPAT=${OEMDEV_MTDFSPAT_LISTS[${OEMDEV_INDEX}]}
					export OEMDEV_CUR_NOTE=${OEMDEV_TYPE_NOTES[${OEMDEV_INDEX}]}
				else
					sdone "     ${OEMDEV_INDEX}     \c"
					sinfo "    ${OEMDEV_TYPE_LISTS[${OEMDEV_INDEX}]}    \c"     
					sinfo "    ${OEMDEV_CPUCODE_LISTS[${OEMDEV_INDEX}]}   \c"     
					sinfo "    ${OEMDEV_APPAREA_LISTS[${OEMDEV_INDEX}]}    \c"     
					sinfo "    ${OEMDEV_MTDHDTYP_LISTS[${OEMDEV_INDEX}]}  \c"
					sinfo "    ${OEMDEV_MTDHDSIZ_LISTS[${OEMDEV_INDEX}]}    \c"
					sinfo "    ${OEMDEV_MTDFSTYP_LISTS[${OEMDEV_INDEX}]}   \c"
					sinfo "    ${OEMDEV_MTDFSPAT_LISTS[${OEMDEV_INDEX}]} \c"
					sinfo "    ${OEMDEV_TYPE_NOTES[${OEMDEV_INDEX}]}\c"
					sinfo "\t  [未选定]"
				fi
			done
			sinfo "-------------------------------------------------------------------------------------"
		else
			show_usage
			exit 1
		fi
		check_choose
		update_configs  
		update_links

		create_curconfig
	fi
}
main_proc $1
